<?php
/**
 * @author Mazvv
 * @package Touroperators
 */
class Touroperators extends CRM_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->a_identifier = 'touroperators';
		$this->lang->load('touroperators');
		$this->load->model('touroperators_model');
		$this->SetNewToolSize('600x450');
		$this->SetEditToolSize('600x450');
		$this->SetDetailsToolSize('600x450');
		$this->SetFindToolSize('600x450');
		
		
		$this->a_validation_rules['touroperator_name'] = "required";
		$this->a_validation_rules['stouroperator_name'] = "required";
		$this->a_validation_rules['license'] = "required";
		//$this->a_validation_rules['chief_name'] = "required";
		//$this->a_validation_rules['www'] = "required";
		$this->a_validation_rules['contract'] = "required";
		$this->a_validation_rules['contract_period'] = "required";
		//$this->a_validation_rules['adress'] = "required";
		
		//$this->a_validation_fields['_cifs_rid'] = $this->lang->line('CIF');
		$this->a_validation_fields['touroperator_name'] = $this->lang->line('NAME');
		$this->a_validation_fields['stouroperator_name'] = $this->lang->line('SNAME');
		$this->a_validation_fields['license'] = $this->lang->line('LICENSE');
		$this->a_validation_fields['www'] = $this->lang->line('WWW');
		$this->a_validation_fields['contract'] = $this->lang->line('CONTRACT');
		$this->a_validation_fields['contract_period'] = $this->lang->line('CONTRACT_PERIOD');
		$this->a_validation_fields['adress'] = $this->lang->line('ADRESS');
		$this->a_validation_fields['fax'] = $this->lang->line('FAX');
		$this->a_validation_fields['contact_person'] = $this->lang->line('CONTACTPERSON');
		$this->a_validation_fields['contact_phone'] = $this->lang->line('CONTACTPHONE');
		$this->a_validation_fields['contact_email'] = $this->lang->line('CONTACTEMAIL');				
		$this->a_validation_fields['chief_name'] = $this->lang->line('CHIEF_NAME');
		
		$this->a_validation_fields['descr'] = $this->lang->line('DESCR');
		$this->a_validation_fields['archive'] = $this->lang->line('ARCHIVE');
		$this->a_validation_fields['modifyDT'] = $this->lang->line('ARCHIVE');
		$this->validation->set_rules($this->a_validation_rules);
		$this->validation->set_fields($this->a_validation_fields);
		
		$this->objectsList['MENU_OBJECT'] = $this->menucontainer->RenderVerticalTreeMenu();
		
	}
	
	public function _remap($m_cif_name){
		switch ($m_cif_name) {
			case 'nJ': {echo $this->_create();break;}
			case 'eJ': {echo $this->_edit();break;}
			case 'dJ': {echo $this->_details();break;}
			case 'rJ': {echo $this->_remove();break;}
			case 'fJ'; {echo $this->_find();break;}
			case 'uJ'; {echo $this->_refresh();break;}
			case 'srt'; {$this->_sort();break;}
			case 'ar'; {$this->_activerecord();break;}
			case 'sar'; {$this->_setactiverecord();break;}
			case 'pg':{echo $this->_journal();break;}
			default: $this->_index();
		}
	}
	
	protected function _index(){
		$this->objectsList['GRID_OBJECT'] = $this->_journal();
		$this->load->view('layouts/main_layout', $this->objectsList);
	}

	public function _journal(){
		
		$content = array();
		$content['title'] = $this->lang->line('DICTIONARY_TITLE'); 
		$content['rid'] = 'ID'; 
		
		$content['stouroperator_name'] = $this->lang->line('SNAME');
		$content['license'] = $this->lang->line('LICENSE');
		$content['contract'] = $this->lang->line('CONTRACT');
		$content['contract_period'] = $this->lang->line('CONTRACT_PERIOD');
		$content['www'] = $this->lang->line('WWW');
		$content['archive'] =  $this->lang->line('ARCHIVE');
		$content['modifyDT'] =  $this->lang->line('MODIFYDT');
		$content['yes'] = $this->lang->line('YES');
		$content['no'] = $this->lang->line('NO');
		$content['tools'] = $this->GetTools(); 
		$content['DS'] = $this->touroperators_model->GetDS();
		$content['pagination'] = $this->GetPagination();
		if($content['DS'] && !$this->GetSessionData('activerecord')) $this->SetSessionData('activerecord', $content['DS'][0]->rid);
		$content['activerecord'] = $this->GetSessionData('activerecord');	
		//echo $content['activerecord'];
		return $this->load->view('touroperators/journal', $content, True);		
	}
	
	private function _create(){
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE'); 

			# { _cifs_rid
			//$content['_cifs_rid'] = $this->RenderLabel('_cifs_rid');
			//$content['_cifs_rid_field'] = form_dropdown('_cifs_rid', $this->cifs_model->GetPairArr(), $this->validation->_cifs_rid?$this->validation->_cifs_rid:'');
			# } _cifs_rid

			# { touroperator_name
			$content['touroperator_name'] = $this->RenderLabel('touroperator_name');
			$content['touroperator_name_field'] =  form_input('touroperator_name', $this->validation->touroperator_name?$this->validation->touroperator_name:'');
			# } touroperator_name

			# { stouroperator_name
			$content['stouroperator_name'] = $this->RenderLabel('stouroperator_name');
			$content['stouroperator_name_field'] =  form_input('stouroperator_name', $this->validation->stouroperator_name?$this->validation->stouroperator_name:'');
			# } stouroperator_name
			
			# { chief_name
			$content['chief_name'] = $this->RenderLabel('chief_name');
			$content['chief_name_field'] =  form_input('chief_name', $this->validation->chief_name?$this->validation->chief_name:'');
			# } chief_name

			# { license
			$content['license'] = $this->RenderLabel('license');
			$content['license_field'] =  form_input('license', $this->validation->license?$this->validation->license:'');
			# } license
						
			# { contact_phone
			$content['contact_phone'] = $this->RenderLabel('contact_phone');
			$content['contact_phone_field'] =  form_input('contact_phone', $this->validation->contact_phone?$this->validation->contact_phone:'');
			# } contact_phone
			
			# { contact_email
			$content['contact_email'] = $this->RenderLabel('contact_email');
			$content['contact_email_field'] =  form_input('contact_email', $this->validation->contact_email?$this->validation->contact_email:'');
			# } contact_email
			
			# { www
			$content['www'] = $this->RenderLabel('www');
			$content['www_field'] =  form_input('www', $this->validation->www?$this->validation->www:'');
			# } www
			
			# { contract
			$content['contract'] = $this->RenderLabel('contract');
			$content['contract_field'] =  form_input('contract', $this->validation->contract?$this->validation->contract:'');
			# } contract

			# { contract_period
			$content['contract_period'] = $this->RenderLabel('contract_period');
			$data = array('name'=>'contract_period', 'id'=>'contract_period', 'size'=>'12', 'value'=>$this->validation->contract_period?$this->validation->contract_period:'', 'readonly'=>'readonly');
			$content['contract_period_field'] =  form_input($data).$this->RenderCalendar('contract_period');
			# } contract_period
			
			# { adress
			$content['adress'] = $this->RenderLabel('adress');
			$content['adress_field'] =  form_input('adress', $this->validation->adress?$this->validation->adress:'');
			# } adress
			
			# { fax
			$content['fax'] = $this->RenderLabel('fax');
			$content['fax_field'] =  form_input('fax', $this->validation->fax?$this->validation->fax:'');
			# } fax
			
			# { contact_person
			$content['contact_person'] = $this->RenderLabel('contact_person');
			$content['contact_person_field'] =  form_input('contact_person', $this->validation->contact_person?$this->validation->contact_person:'');
			# } contact_person
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:''); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			# } modifyDT
			return $this->load->view('touroperators/create', $content, True);
		}
		if($insRid = $this->touroperators_model->CreateRecord()){
			$this->SetSessionData('activerecord', $insRid);
			$this->load->view('standart/tmpl/create_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _edit(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['DS'] = $this->touroperators_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_EDIT')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT'); 
			
			# { touroperator_name
			$content['touroperator_name'] = $this->RenderLabel('touroperator_name');
			$content['touroperator_name_field'] =  form_input('touroperator_name', $this->validation->touroperator_name?$this->validation->touroperator_name:$content['DS']->touroperator_name);
			# { touroperator_name

			# { stouroperator_name
			$content['stouroperator_name'] = $this->RenderLabel('stouroperator_name');
			$content['stouroperator_name_field'] =  form_input('stouroperator_name', $this->validation->stouroperator_name?$this->validation->stouroperator_name:$content['DS']->stouroperator_name);
			# { stouroperator_name
			
			# { license
			$content['license'] = $this->RenderLabel('license');
			$content['license_field'] =  form_input('license', $this->validation->license?$this->validation->license:$content['DS']->license);
			# { license
			
			# { chief_name
			$content['chief_name'] = $this->RenderLabel('chief_name');
			$content['chief_name_field'] =  form_input('chief_name', $this->validation->chief_name?$this->validation->chief_name:$content['DS']->chief_name);
			# } chief_name

			# { contact_phone			
			$content['contact_phone'] = $this->RenderLabel('contact_phone');
			$content['contact_phone_field'] =  form_input('contact_phone', $this->validation->contact_phone?$this->validation->contact_phone:$content['DS']->contact_phone);
			# } contact_phone

			# { contact_email
			$content['contact_email'] = $this->RenderLabel('contact_email');
			$content['contact_email_field'] =  form_input('contact_email', $this->validation->contact_email?$this->validation->contact_email:$content['DS']->contact_email);
			# } contact_email

			# { www
			$content['www'] = $this->RenderLabel('www');
			$content['www_field'] =  form_input('www', $this->validation->www?$this->validation->www:$content['DS']->www);
			# } www
			
			# { contract
			$content['contract'] = $this->RenderLabel('contract');
			$content['contract_field'] =  form_input('contract', $this->validation->contract?$this->validation->contract:$content['DS']->contract);
			# } contract
			
			# { contract_period
			$content['contract_period'] = $this->RenderLabel('contract_period');
			$data = array('name'=>'contract_period', 'id'=>'contract_period', 'size'=>'12', 'value'=>$this->validation->contract_period?$this->validation->contract_period:$content['DS']->contract_period, 'readonly'=>'readonly');
			$content['contract_period_field'] =  form_input($data).$this->RenderCalendar('contract_period');
			# } contract_period
			
			# { adress
			$content['adress'] = $this->RenderLabel('adress');
			$content['adress_field'] =  form_input('adress', $this->validation->adress?$this->validation->adress:$content['DS']->adress);
			# } adress
			
			# { fax
			$content['fax'] = $this->RenderLabel('fax');
			$content['fax_field'] =  form_input('fax', $this->validation->fax?$this->validation->fax:$content['DS']->fax);
			# } fax
			
			# { contact_person
			$content['contact_person'] = $this->RenderLabel('contact_person');
			$content['contact_person_field'] =  form_input('contact_person', $this->validation->contact_person?$this->validation->contact_person:$content['DS']->contact_person);
			# } contact_person
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$content['descr_field'] =  form_input('descr', $this->validation->descr?$this->validation->descr:$content['DS']->descr);
			# } descr
			
			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			return $this->load->view('touroperators/edit', $content, True);
		}
		if($this->touroperators_model->UpdateRecord()){
			$this->load->view('standart/tmpl/edit_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _details(){
			if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
			$content = array();
			$content['DS'] = $this->touroperators_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_DETAILS')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_DETAILS'); 
			
			# { touroperator_name
			$content['touroperator_name'] = $this->RenderLabel('touroperator_name');
			$content['touroperator_name_field'] =  form_input('touroperator_name', $this->validation->touroperator_name?$this->validation->touroperator_name:$content['DS']->touroperator_name);
			# { touroperator_name

			# { stouroperator_name
			$content['stouroperator_name'] = $this->RenderLabel('stouroperator_name');
			$content['stouroperator_name_field'] =  form_input('stouroperator_name', $this->validation->stouroperator_name?$this->validation->stouroperator_name:$content['DS']->stouroperator_name);
			# { stouroperator_name
			
			# { license
			$content['license'] = $this->RenderLabel('license');
			$content['license_field'] =  form_input('license', $this->validation->license?$this->validation->license:$content['DS']->license);
			# { license
			
			# { chief_name
			$content['chief_name'] = $this->RenderLabel('chief_name');
			$content['chief_name_field'] =  form_input('chief_name', $this->validation->chief_name?$this->validation->chief_name:$content['DS']->chief_name);
			# } chief_name

			# { contact_phone			
			$content['contact_phone'] = $this->RenderLabel('contact_phone');
			$content['contact_phone_field'] =  form_input('contact_phone', $this->validation->contact_phone?$this->validation->contact_phone:$content['DS']->contact_phone);
			# } contact_phone

			# { contact_email
			$content['contact_email'] = $this->RenderLabel('contact_email');
			$content['contact_email_field'] =  form_input('contact_email', $this->validation->contact_email?$this->validation->contact_email:$content['DS']->contact_email);
			# } contact_email

			# { www
			$content['www'] = $this->RenderLabel('www');
			$content['www_field'] =  form_input('www', $this->validation->www?$this->validation->www:$content['DS']->www);
			# } www
			
			# { contract
			$content['contract'] = $this->RenderLabel('contract');
			$content['contract_field'] =  form_input('contract', $this->validation->contract?$this->validation->contract:$content['DS']->contract);
			# } contract
			
			# { contract_period
			$content['contract_period'] = $this->RenderLabel('contract_period');
			$data = array('name'=>'contract_period', 'id'=>'contract_period', 'size'=>'12', 'value'=>$this->validation->contract_period?$this->validation->contract_period:$content['DS']->contract_period, 'readonly'=>'readonly');
			$content['contract_period_field'] =  form_input($data).$this->RenderCalendar('contract_period');
			# } contract_period
			
			# { adress
			$content['adress'] = $this->RenderLabel('adress');
			$content['adress_field'] =  form_input('adress', $this->validation->adress?$this->validation->adress:$content['DS']->adress);
			# } adress
			
			# { fax
			$content['fax'] = $this->RenderLabel('fax');
			$content['fax_field'] =  form_input('fax', $this->validation->fax?$this->validation->fax:$content['DS']->fax);
			# } fax
			
			# { contact_person
			$content['contact_person'] = $this->RenderLabel('contact_person');
			$content['contact_person_field'] =  form_input('contact_person', $this->validation->contact_person?$this->validation->contact_person:$content['DS']->contact_person);
			# } contact_person
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$content['descr_field'] =  form_input('descr', $this->validation->descr?$this->validation->descr:$content['DS']->descr);
			# } descr
			
			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			return $this->load->view('touroperators/details', $content, True);
	}

	private function _remove(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->touroperators_model->DeleteRecord();
		$this->SetSessionData('activerecord', null);
		echo $this->_journal();
	}
	
	private function _find(){
	}
	
	private function _refresh(){
		$this->RefreshSessionData();
		echo $this->_journal();
	}
}

?>